<?php

namespace App\Admin\Controllers;

use App\Models\Score;
use App\Models\Semester;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;


class ScoreController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('教务管理')
            ->description('成绩查询')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('教务管理')
            ->description('成绩详情信息')
            ->body($this->detail($id));

    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
        ->header('教务管理')
        ->description('编辑成绩信息')
        ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('教务管理')
            ->description('新建成绩信息')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Score);

        $grid->id('Id')->sortable();
        $grid->column('semester.name','学期');
        $grid->exam_type('考试类型');
        $grid->column('student.name', '学生');
        $grid->column('course.course_name','课程名称');
        $grid->column('course.total','总分');
        $grid->degree('成绩')->editable()->sortable();
        $grid->column('teacher.name','录入教师');
        $grid->disableCreateButton();
        $grid->actions(function ($actions) {
        $actions->disableView();
        $actions->disableEdit();
    });
        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->column(1/2, function ($filter) {

                $semester_arr = [];
                $semesters = Semester::get();

                foreach ($semesters as $semester) {
                    $semester_arr[$semester->id] = $semester->name;
                }
                $filter->equal('semester_id', '学期')->select($semester_arr);
                $filter->equal('exam_type', '考试类型')->select(['第一次月考' => '第一次月考', '第二次月考' => '第二次月考','期中考试' => '期中考试','期末考试' => '期末考试']);
                $filter->like('course.course_name', '课程名称');
            });
            $filter->column(1/2, function ($filter) {
                $filter->like('student.name', '学生');
                $filter->like('teacher.name', '录入教师');
                $filter->group('degree','成绩', function ($group) {
                    $group->gt('大于');
                    $group->lt('小于');
                    $group->nlt('不小于');
                    $group->ngt('不大于');
                    $group->equal('等于');
                });
            });
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Score::findOrFail($id));


        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Score());

        $form->number('degree','分数');

//        $form->tools(function (Form\Tools $tools) {
//            // 去掉`查看`按钮
//            $tools->disableView();
//        });
        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });

        return $form;
    }
}
